﻿//
// <author>David Nohejl</author>
//
// Contains handlers to menu events in MainWindow. 
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using WPFTrees.Layouts;
using System.Windows.Shapes;
using System.Windows.Controls;
using WPFTrees.Parser;
using Microsoft.Win32;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Media;
using WPFTrees.LabelOptimization;
using System.Drawing;

namespace WPFTrees
{
    public partial class MainWindow : Window
    {     

        #region File

        private void miExportAsBmp_Click(object sender, RoutedEventArgs e)
        {
            // zobrazi ukladaci dialog a ulozi obsah workspace jako bitmapu
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = "Bitmap files |*.bmp";
            dlg.AddExtension = true;

            if (dlg.ShowDialog() == true)
            {
                RenderTargetBitmap bmp = new RenderTargetBitmap((int)parentCanvas.ActualWidth,
                    (int)parentCanvas.ActualHeight, 96, 96, PixelFormats.Pbgra32);
                bmp.Render(parentCanvas);
                
                BmpBitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bmp));
                using (FileStream stream = File.Create(dlg.FileName))
                {
                    encoder.Save(stream);
                }
                
            }
        }

        private void miSaveWorkspace_Click(object sender, RoutedEventArgs e)
        {            
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "WPF Trees Workspace files (*.wtw)|*.wtw";
            if (dlg.ShowDialog() == true)
            {
                Workspace.WorkspaceManager.Save(dlg.FileName);
            }
        }

        private void miLoadWorkspace_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "WPF Trees Workspace files (*.wtw)|*.wtw";
            if (dlg.ShowDialog() == true)
            {
                Workspace.WorkspaceManager.Load(dlg.FileName);
            }
        }

        private void miExit_Click(object sender, RoutedEventArgs e)
        {
            CloseOnExecute(this, null);
        }

        private void miOpen_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }  
        #endregion

        #region Tree

        private void miUseBranchLenghts_Click(object sender, RoutedEventArgs e)
        {
            (layout as ICanUseBranchLengths).UseBranchLenghts = miUseBranchLenghts.IsChecked;
            layout.ExecuteLayout(canvas, tree);
        }

        private void miUnroot_Click(object sender, RoutedEventArgs e)
        {
            (layout as ISupportUnrooting).IsRooted = !miUnroot.IsChecked;
            layout.ExecuteLayout(canvas, tree);
        }

        private void miTreeProperties_Click(object sender, RoutedEventArgs e)
        {
            ShowTreePropertiesOnExecute(sender, null);
        }

        #endregion

        #region View -> Toolbars (Toolbar visibility)

        private void miNavigation_Checked(object sender, RoutedEventArgs e)
        {
            if (tlbNavigation != null)
            {
                tlbNavigation.Visibility = Visibility.Visible;

            }
        }

        private void miNavigation_UnChecked(object sender, RoutedEventArgs e)
        {
            if (tlbNavigation != null)
            {
                tlbNavigation.Visibility = Visibility.Collapsed;

            }
        }

        private void miInserts_Checked(object sender, RoutedEventArgs e)
        {
            if (tblInserts != null)
            {
                tblInserts.Visibility = Visibility.Visible;

            }
        }

        private void miInserts_UnChecked(object sender, RoutedEventArgs e)
        {
            if (tblInserts != null)
            {
                tblInserts.Visibility = Visibility.Collapsed;

            }
        }

        private void miSearch_Checked(object sender, RoutedEventArgs e)
        {
            if (tblSearch != null)
            {
                tblSearch.Visibility = Visibility.Visible;

            }
        }

        private void miSearch_UnChecked(object sender, RoutedEventArgs e)
        {
            if (tblSearch != null)
            {
                tblSearch.Visibility = Visibility.Collapsed;

            }
        }

        private void miLayouts_Checked(object sender, RoutedEventArgs e)
        {
            if (tblLayouts != null)
            {
                tblLayouts.Visibility = Visibility.Visible;

            }
        }

        private void miLayouts_UnChecked(object sender, RoutedEventArgs e)
        {
            if (tblLayouts != null)
            {
                tblLayouts.Visibility = Visibility.Collapsed;

            }
        }

        private void miStandard_Checked(object sender, RoutedEventArgs e)
        {
            if (tlbStandard != null)
            {
                tlbStandard.Visibility = Visibility.Visible;

            }
        }

        private void miStandard_UnChecked(object sender, RoutedEventArgs e)
        {
            if (tlbStandard != null)
            {
                tlbStandard.Visibility = Visibility.Collapsed;
            }
        }

        #endregion

        #region View -> Labels

        private void miShowInternalLabels_Click(object sender, RoutedEventArgs e)
        {
            RenderTree(CurrentTree);
        }

        bool bRotateLabels;
        private void miRotateLabels_Click(object sender, RoutedEventArgs e)
        {
            bRotateLabels = (sender as MenuItem).IsChecked;
        }

        private void miShowLabels_Click(object sender, RoutedEventArgs e)
        {
            RenderTree(CurrentTree);
        }

        private void miFont_Click(object sender, RoutedEventArgs e)
        {
            FontDialogSample.FontChooser chooser = new FontDialogSample.FontChooser();
            chooser.SelectedFontFamily = DefaultSettings.Default.LabelFontFamily;

            if (chooser.ShowDialog() == true)
            {
                DefaultSettings.Default.LabelFontFamily = chooser.SelectedFontFamily;
                DefaultSettings.Default.LabelFontSize = chooser.SelectedFontSize;
            }
        }

        private void miOptimizeLabels_Click(object sender, RoutedEventArgs e)
        {
            LabelOptimizer.Optimize(tree.Info.Leaves,canvas,new AnnealingSettings());
        }

        #endregion

        #region Tools

        private void miSettings_Click(object sender, RoutedEventArgs e)
        {
            Shape shape = CreateLeafNodeShape(new Canvas(), new NewickNode("", true));
            SettingsDlg dlg = new SettingsDlg(shape);
            dlg.InnerNodeSize = DefaultSettings.Default.InnerNodeHeight;
            dlg.LeafNodeSize = DefaultSettings.Default.LeafNodeHeight;
            if (dlg.ShowDialog() == true)
            {
                DefaultSettings.Default.InnerNodeHeight = dlg.InnerNodeSize;
                DefaultSettings.Default.LeafNodeHeight = dlg.LeafNodeSize;
            }
        }

        #endregion

        #region Help

        private void miContents_Click(object sender, RoutedEventArgs e)
        {
            HelpRequestedOnExecute(sender, null);
        }

        private void miAbout_Click(object sender, RoutedEventArgs e)
        {
            AboutWindow dlgAbout = new AboutWindow();
            dlgAbout.Owner = this;
            dlgAbout.ShowDialog();
        }

        #endregion


        #region Layouts toolbar

        private void miCladogram_Click(object sender, RoutedEventArgs e)
        {
            ChangeLayout(new CladogramLayout());
        }

        private void miRectangular_Click(object sender, RoutedEventArgs e)
        {
            ChangeLayout(new RectangularLayout());
        }

        private void miRadial_Click(object sender, RoutedEventArgs e)
        {
            ChangeLayout(new RadialLayout());
        }

        private void miRadialClassic_Click(object sender, RoutedEventArgs e)
        {
            ChangeLayout(new ClassicRadialLayout());
        }

        #endregion

        #region Navigation toolbar
        
        private void miRotateLeft_Click(object sender, RoutedEventArgs e)
        {
            //TODO: custom command for rotation
            RotateLeftOnExecute(sender, null);
        }

        private void miRotateRight_Click(object sender, RoutedEventArgs e)
        {
            //TODO: custom command for rotation
            RotateRightOnExecute(sender, null);
        }              

        private void miCenterView_Click(object sender, RoutedEventArgs e)
        {
            CenterTree();
        }

        private void miCenterTree_Click(object sender, RoutedEventArgs e)
        {
            CenterTree();
        }    

        #endregion    

        #region Standard toolbar

        private void miPrint_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = (CurrentTree != null);
        }

        #endregion

        #region Search toolbar

        private void miFind_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = (CurrentTree != null);
        }

        #endregion

        private void miColors_Click(object sender, RoutedEventArgs e)
        {
            ColorsDlg dlg = new ColorsDlg();
            dlg.ShowDialog();
        }

        private void miZoom_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = (CurrentTree != null);
        }                     
    }
}
